package com.jari.lg.service.analysis.device;

import com.jari.lg.domain.DevBase;
import com.jari.lg.domain.DevErrDayHis;
import com.jari.lg.domain.DevErrMonthHis;
import com.jari.lg.mapper.DevErrDayHisMapper;
import com.jari.lg.mapper.DevErrMonthHisMapper;
import com.jari.lg.mapper.DevErrQuarterHisMapper;
import com.jari.lg.utils.BaseData;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DevErrHisService {
    @Resource
    private DevErrDayHisMapper devErrDayHisMapper;
    @Resource
    private DevErrMonthHisMapper devErrMonthHisMapper;
    @Resource
    private DevErrQuarterHisMapper devErrQuarterHisMapper;

    /*    @Scheduled(cron = "0 0/5 * * * ? ")*/
    protected void CreateDevErrHis() throws ParseException {
        List<DevErrDayHis> ansDay = new ArrayList<>();
        List<DevErrMonthHis> ansMonth = new ArrayList<>();

        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(format1.parse("2022-01-01 00:00:00"));//从2022年1月开始生产数据
        List<DevBase> devBaseList = BaseData.devBaseList;
        //随机设置n个设备存在各样的离线情况，其他设备默认一直没有离线
        for (int i = 0; i < 1444; i += 20) {
            DevBase devBase = devBaseList.get(i);
            for (int m = 0; m < 3; m++) {
                DevErrMonthHis devErrMonthHis = new DevErrMonthHis();
                devErrMonthHis.setDevcode(devBase.getDevicecode());
                devErrMonthHis.setDevname(devBase.getDevicename());
                devErrMonthHis.setDevtype(Integer.parseInt(devBase.getDevicetype()));
                devErrMonthHis.setManufacture(devBase.getManufacturer());
                devErrMonthHis.setStartdate(calendar.getTime());
                //单个设备每天以及每个月数据生产，202201--202203
                int dayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

                for (int j = 0; j < dayOfMonth; j++) {
                    DevErrDayHis devErrDayHis = new DevErrDayHis();
                    devErrDayHis.setDevcode(devBase.getDevicecode());
                    devErrDayHis.setDevname(devBase.getDevicename());
                    devErrDayHis.setDevtype(Integer.parseInt(devBase.getDevicetype()));
                    devErrDayHis.setManufacture(devBase.getManufacturer());
                    devErrDayHis.setTimes((new Random().nextInt(10)) % 10 + 1);//每天故障1-10次之间
                    devErrMonthHis.setTimes(devErrMonthHis.getTimes() + devErrDayHis.getTimes());

                    devErrDayHis.setStartdate(calendar.getTime());
                    calendar.add(Calendar.DATE, 1);//时间+1天
                    ansDay.add(devErrDayHis);
                }
                ansMonth.add(devErrMonthHis);
            }
            //一个设备数据生产结束，开始下一个设备
            calendar.setTime(format1.parse("2022-01-01 00:00:00"));
        }
        devErrDayHisMapper.insertBatch(ansDay);
        devErrMonthHisMapper.insertBatch(ansMonth);
    }

}
